package com.hourse.platform.service.impl;

import com.hourse.platform.common.ErrorMsg;
import com.hourse.platform.entity.*;
import com.hourse.platform.exception.AppException;
import com.hourse.platform.service.FlowService;
import com.hourse.platform.utils.*;
import com.hourse.platform.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class FlowServiceImpl extends PublicServiceImpl implements FlowService {
  /**
   * @功能描述: 【获取流程列表】
   * @作 者 :
   * @创建时间 :
   */
  @Transactional(readOnly = true)
  @Override
  public List<FlowVO> flowList() {

    SqlParams sqlParams = SqlParams.New();
    sqlParams.append(" select ");
    sqlParams.append(" new com.hourse.platform.vo.FlowVO(a.id,a.name,a.actionBeanName,a.createTime)");
    sqlParams.append(" from FlowEntity a order by a.createTime desc");
    return baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());
  }

  /**
   * @功能描述: 【流程节点列表】
   * @作 者 :
   * @创建时间 :
   */
  @Transactional(readOnly = true)
  @Override
  public List<FlowNodeVO> nodeList(String flowId) {

    SqlParams sqlParams = SqlParams.New();
    sqlParams.append(" select ");
    sqlParams.append(" new com.hourse.platform.vo.FlowNodeVO(");
    sqlParams.append(" a.id,a.flowId,a.name,a.sort,a.type,a.roleCode,a.otherCode,a.nodeCode,a.createTime");
    sqlParams.append(" ) from FlowNodeEntity a ");
    sqlParams.append(" where a.flowId = :flowId order by a.sort");
    sqlParams.add("flowId",flowId);
    return baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());
  }

  /**
   * @param flowId
   * @功能描述: 【获取流程绑定信息】
   * @作 者 : Rain
   * @创建时间 : 2020/12/14 20:16
   */
  @Transactional(readOnly = true)
  @Override
  public List<FlowBindInfoVO> bindInfoList(String flowId) {

    SqlParams sqlParams = SqlParams.New();
    sqlParams.append(" select ");
    sqlParams.append(" new com.hourse.platform.vo.FlowBindInfoVO(");
    sqlParams.append(" a.id,a.flowId,a.menuId,b.name");
    sqlParams.append(" ) from FlowBindInfoEntity a  ");
    sqlParams.append("  left join MenuEntity b on a.menuId = b.id");
    sqlParams.append(" where a.flowId = :flowId order by a.createTime desc");
    sqlParams.add("flowId",flowId);
    return baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());
  }

  /**
   * @功能描述: 【获取菜单绑定流程】
   * @作 者 : Rain
   * @创建时间 : 2020/12/15 13:00
   */
  @Transactional(readOnly = true)
  @Override
  public List<FlowBindInfoVO> bindInfoListByMenuId(String menuId) {

    SqlParams sqlParams = SqlParams.New();
    sqlParams.append(" select ");
    sqlParams.append(" new com.hourse.platform.vo.FlowBindInfoVO(");
    sqlParams.append(" a.flowId,b.name");
    sqlParams.append(" ) from FlowBindInfoEntity a  ");
    sqlParams.append("  left join FlowEntity b on a.flowId = b.id");
    sqlParams.append(" where a.menuId = :menuId order by a.createTime desc");
    sqlParams.add("menuId",menuId);
    return baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());
  }

  /**
   * @param flowId
   * @param instanceId
   * @功能描述: 【获取实例下一处理节点】
   * @作 者 : Rain
   * @创建时间 : 2020/12/15 15:38
   */
  @Transactional(readOnly = true)
  @Override
  public List<FlowNodeEntity> getInstanceNextNode(String flowId, String instanceId) {

    SqlParams sqlParams = SqlParams.New();
    if(StringTools.isNull(instanceId)){
      sqlParams.append(" from FlowNodeEntity a where a.flowId = :flowId");
      sqlParams.append(" and a.type != 'START' ");
      sqlParams.append(" order by a.sort");
      sqlParams.add("flowId",flowId);

      List<FlowNodeEntity> list =baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());

      if(AppTools.isAdmin()){
        return list;
      }  else {
        return list.subList(0,1);
      }

    } else {
      String currentNodeId = getCurrentNodeId(flowId,instanceId);

      return  getFlowInstanceNextNode(flowId,currentNodeId);
    }
  }

  /**
   * @功能描述: 【获取实例下一处理节点】
   * @作 者 : Rain
   * @创建时间 : 2020/12/15 15:38
   */
  @Transactional(readOnly = true)
  List<FlowNodeEntity> getFlowInstanceNextNode(String flowId,String currentNodeId) {

      SqlParams sqlParams = SqlParams.New();
      sqlParams.append(" from FlowNodeEntity a where a.flowId = :flowId");
      sqlParams.append(" order by a.sort");
      sqlParams.add("flowId",flowId);

      List<FlowNodeEntity> nodeList = baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());
      List<FlowNodeEntity> repList = new ArrayList();

//      if(AppTools.isAdmin()){
//
//        FlowNodeEntity nextNode = null;
//
//        for(int i = 0 ; i < nodeList.size() ; i++){
//          if(currentNodeId.equals(nodeList.get(i).getId())){
//              if(!"END".equals(nodeList.get(i).getType())){
//                nextNode = nodeList.get(i + 1);
//              }
//              repList.add(nodeList.get(i));
//          } else if(nodeList.get(i) != nextNode){
//              repList.add(nodeList.get(i));
//          }
//        }
//
//        repList.add(0,nextNode);
//
//      } else {
        for(int i = 0 ; i < nodeList.size() ; i++){
          if(currentNodeId.equals(nodeList.get(i).getId())){
            repList.add(nodeList.get(i+1));
            return repList;
          }
        }
//      }
    return repList;
  }

  /**
   * @功能描述: 【获取流程开始节点】
   * @作 者 : Rain
   * @创建时间 : 2020/12/15 15:38
   */
  @Override
  @Transactional(readOnly = true)
  public String getFlowFirstNodeId(String flowId) {

    SqlParams sqlParams = SqlParams.New();
    sqlParams.append(" from FlowNodeEntity a where a.flowId = :flowId");
    sqlParams.append(" order by a.sort");
    sqlParams.add("flowId",flowId);

    List<FlowNodeEntity> list = baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());

    if(list != null && list.size() >0){

      log.info("【当前节点信息】:{}:{}",list.get(0).getName(),list.get(0).getId());
      return list.get(0).getId();
    }

    return null;
  }

  /**
   * @功能描述: 【获取流程操作信息】
   * @作   者 : Rain
   * @创建时间 : 2020/12/17 13:32
   */
  @Transactional(readOnly = true)
  @Override
  public List<FlowProcessEntity> flowOprateList(String instanceId) {

    SqlParams sqlParams = SqlParams.New();
    sqlParams.append(" from FlowProcessEntity a where a.instanceId = :instanceId");
    sqlParams.append(" order by a.createTime desc");
    sqlParams.add("instanceId",instanceId);

    return baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());
  }

  /**
   * @功能描述: 【获取流程参数】
   * @作 者 : Rain
   * @创建时间 : 2020/12/20 23:21
   */
  @Transactional(readOnly = true)
  @Override
  public List<FlowParamsEntity> flowParamsList(String flowId) {

    SqlParams sqlParams = SqlParams.New();
    sqlParams.append(" from FlowParamsEntity a where a.flowId = :flowId");
    sqlParams.append(" order by a.attr");
    sqlParams.add("flowId",flowId);

    return baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());
  }

  /**
   * @功能描述: 【删除流程属性】
   * @作 者 : Rain
   * @创建时间 : 2020/12/20 23:30
   */
  @Transactional(rollbackFor = AppException.class)
  @Override
  public void deleteFlowParams(String id) throws AppException {

    try {
      SqlParams sqlParams = SqlParams.New();
      sqlParams.append("delete from flow_params where id = :id");
      sqlParams.add("id",id);
      baseDao.executeSqlUpdate(sqlParams.getSql(),sqlParams.getParams());
      sqlParams.reset();

      sqlParams.append("delete from flow_node_params where params_id = :id");
      sqlParams.add("id",id);
      baseDao.executeSqlUpdate(sqlParams.getSql(),sqlParams.getParams());

    }catch (Exception e){
      throw new AppException(e.getMessage());
    }
  }

  /**
   * @功能描述: 【获取点参数配置表】
   * @作 者 : Rain
   * @创建时间 : 2020/12/21 12:48
   */
  @Transactional(readOnly = true)
  @Override
  public List<FlowNodeParamsEntity> flowNodeParamsList(String flowId,String nodeId) {

    SqlParams sqlParams = SqlParams.New();
    sqlParams.append(" select ");
    sqlParams.append(" new com.hourse.platform.vo.FlowNodeParamsVO( ");
    sqlParams.append(" a.id,a.name,a.attr,a.val,b.id");
    sqlParams.append(" ) from FlowParamsEntity a ");
    sqlParams.append(" left join FlowNodeParamsEntity b on a.id = b.paramsId and b.nodeId = :nodeId");
    sqlParams.append(" where a.flowId = :flowId order by a.attr");
    sqlParams.add("flowId",flowId);
    sqlParams.add("nodeId",nodeId);

    return baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());
  }

  /**
   * @功能描述: 【删除节点属性】
   * @作 者 : Rain
   * @创建时间 : 2020/12/21 13:55
   */
  @Transactional
  @Override
  public void deleteFlowNodeParams(String nodeId, String paramsId) throws AppException {

    try {

      SqlParams sqlParams = SqlParams.New();
      sqlParams.append("delete from flow_node_params ");
      sqlParams.append("where node_id = :nodeId  and params_id = :paramsId");

      sqlParams.add("nodeId",nodeId);
      sqlParams.add("paramsId",paramsId);

      baseDao.executeSqlUpdate(sqlParams.getSql(),sqlParams.getParams());
    }catch (Exception e){
      e.printStackTrace();
      throw new AppException(e.getMessage());
    }
  }

  /**
   * @param id
   * @功能描述: 【获取节点参数】
   * @作 者 : Rain
   * @创建时间 : 2020/12/21 14:07
   */
  @Transactional(readOnly = true)
  @Override
  public List<FlowNodeParamsVO> getNodeParams(String id) {

    SqlParams sqlParams = SqlParams.New();
    sqlParams.append(" select ");
    sqlParams.append(" new com.hourse.platform.vo.FlowNodeParamsVO( ");
    sqlParams.append(" a.id,b.name,b.attr,b.val,b.id");
    sqlParams.append(" ) from FlowNodeParamsEntity a ");
    sqlParams.append(" left join FlowParamsEntity b on b.id = a.paramsId");
    sqlParams.append(" where a.nodeId = :nodeId ");
    sqlParams.add("nodeId",id);

    return baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());
  }

  /**
   * @功能描述: 【删除流程节点】
   * @作 者 : Rain
   * @创建时间 : 2020/12/24 14:56
   */
  @Transactional(rollbackFor = AppException.class)
  @Override
  public void deleteFlowNode(String nodeId) throws AppException {
    try {

      baseDao.deleteObjectSerializableKey(FlowNodeEntity.class,nodeId);
      SqlParams sqlParams = new SqlParams();
      sqlParams.append("delete from flow_node_params where node_id = :nodeId");
      sqlParams.add("nodeId",nodeId);
      baseDao.executeSqlUpdate(sqlParams.getSql(),sqlParams.getParams());
    }catch (Exception e){
      e.printStackTrace();
      throw new AppException(e.getMessage());
    }
  }

  /**
   * @param flowId
   * @param nodeId
   * @功能描述: 【获取绑定子流程列表】
   * @作 者 : Rain
   * @创建时间 : 2021/1/26 22:30
   */
  @Override
  @Transactional(readOnly = true)
  public List<FlowChildrenBindEntity> getBindFlowList(String flowId, String nodeId) {

    SqlParams sqlParams = SqlParams.New();
    sqlParams.append(" select new com.hourse.platform.vo.FlowChildrenBindVO(");
    sqlParams.append("   a.id, a.flowId, a.nodeId, a.bindFlowId, a.slot, f.name");
    sqlParams.append(") from FlowChildrenBindEntity a ");
    sqlParams.append(" left join FlowEntity f on a.bindFlowId = f.id");
    sqlParams.append(" where a.flowId = :flowId and a.nodeId = :nodeId");
    sqlParams.add("flowId",flowId);
    sqlParams.add("nodeId",nodeId);

    return baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());
  }

  /**
   * @功能描述: 【流程节点列表】
   * @作 者 : Rain
   * @创建时间 : 2020/12/13 16:35
   */
  @Transactional(readOnly = true)
  @Override
  public List<FlowNodeVO> nodeList(String flowId, String instanceId) {

    if(!StringTools.isNull(instanceId)){

      FlowPath flowPath = (FlowPath)baseDao.getObjectBySerializableKey(FlowPath.class,instanceId);

      List<FlowNodeVO> tempList = new ArrayList<>();

      if(flowPath != null){

       List<FlowNodeVO> masterNodeList =  nodeList(flowPath.getMasterFlowId());
       List<FlowNodeVO> branchNodeList =  nodeList(flowPath.getBranchFlowId());

        for(FlowNodeVO node: masterNodeList){

         if("FLOW".equals(node.getType())){
           if(branchNodeList != null && branchNodeList.size() > 0){
             tempList.addAll(branchNodeList.subList(0,branchNodeList.size() - 1));
           } else {
             tempList.add(node);
           }

         } else {
           tempList.add(node);
         }
       }

       return tempList;
      }
    }

    return nodeList(flowId);
  }


  /**
 * @功能描述: 【获取当前节点】
 * @作   者 : Rain
 * @创建时间 : 2020/12/15 17:38
 */
  @Override
  @Transactional(readOnly = true)
  public String getCurrentNodeId(String flowId, String instanceId){

    SqlParams sqlParams = SqlParams.New();
    sqlParams.append(" from FlowProcessEntity a where a.flowId =:flowId");
    sqlParams.append(" and a.instanceId =:instanceId order by a.createTime desc");
    sqlParams.add("instanceId",instanceId);
    sqlParams.add("flowId",flowId);

    List<FlowProcessEntity> list = baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());

    if(list != null && list.size() > 0){

        log.info("【当前节点信息】:{}:{}",list.get(0).getNextNodeName(),list.get(0).getNextNodeId());
        return list.get(0).getNextNodeId();
    } else {
        return getFlowFirstNodeId(flowId);
    }
  }

  @Transactional(rollbackFor = AppException.class)
  FlowProcessService changeBranch(FlowEntity currentFlow, FlowProcessVO flowProcessVO) throws Exception {

    FlowProcessService processService = null;

    // 获取当前节点
    FlowNodeEntity currentNode =
      (FlowNodeEntity)baseDao.getObjectBySerializableKey(FlowNodeEntity.class,
        getCurrentNodeId(flowProcessVO.getFlowId(), flowProcessVO.getInstanceId()));

    // 获取节点列表
    List<FlowNodeEntity> nodeList =
      getFlowInstanceNextNode(flowProcessVO.getFlowId(), currentNode.getId());

    // 若发送至结束节点必 须切回至 MASTER
    if("END".equals(nodeList.get(0).getType())){
      FlowPath flowPath = (FlowPath)baseDao.getObjectBySerializableKey(FlowPath.class,flowProcessVO.getInstanceId());
      flowPath.setStatus("MASTER");
      baseDao.saveOrUpdate(flowPath);
      baseDao.currentSessionflush();
    }

    if(StringTools.isNotNull(currentFlow.getActionBeanName())){

      processService = (FlowProcessService)SpringContext.getBean(currentFlow.getActionBeanName(),FlowProcessService.class);


      if("FLOW".equals(nodeList.get(0).getType())){

        String flowSlot = processService.changeBranch(flowProcessVO.getInstanceId(),currentNode.getNodeCode());
        if(StringTools.isNotNull(flowSlot)){

          SqlParams sqlParams = SqlParams.New();
          sqlParams.append(" from FlowChildrenBindEntity a where a.flowId = :flowId and a.nodeId = :nodeId and a.slot =:slot");
          sqlParams.add("flowId",currentNode.getFlowId());
          sqlParams.add("nodeId",nodeList.get(0).getId());
          sqlParams.add("slot",flowSlot);

          List<FlowChildrenBindEntity> flowChildrenBindEntity = baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());
          // 获取指定的分支
          FlowChildrenBindEntity f = flowChildrenBindEntity.get(0);

          FlowPath flowPath = (FlowPath)baseDao.getObjectBySerializableKey(FlowPath.class,flowProcessVO.getInstanceId());
          flowPath.setBranchFlowId(f.getBindFlowId());
          flowPath.setStatus("BRANCH");
          flowPath.setChangePathNodeId(nodeList.get(0).getId());
          baseDao.saveOrUpdate(flowPath);

          FlowProcessEntity flowProcessEntity =
            (FlowProcessEntity)flowProcessVO.bindEnity(FlowProcessEntity.class);
          flowProcessEntity.setCreateTime(new Date());
          flowProcessEntity.setCreateUserId(AppTools.GetCurrentUserId());
          flowProcessEntity.setCurrentNodeId(currentNode.getId());
          flowProcessEntity.setCurrentNodeName(currentNode.getName());
          flowProcessEntity.setCurrentNodeType(currentNode.getType());
          flowProcessEntity.setNextNodeId(getFlowFirstNodeId(f.getBindFlowId()));
          flowProcessEntity.setFlowId(f.getBindFlowId());
          baseDao.saveObject(flowProcessEntity);
          baseDao.currentSessionflush();
        }
      }
    }

    return processService;
  }


  /**
   * @功能描述: 【发送流程】
   * @作 者 : Rain
   * @创建时间 : 2020/12/15 17:06
   */
  @Transactional(rollbackFor = AppException.class)
  @Override
  public void sendFlow(FlowProcessVO flowProcessVO) throws AppException {
    try {

      FlowEntity currentFlow =
        (FlowEntity)baseDao.getObjectBySerializableKey(FlowEntity.class,flowProcessVO.getFlowId());

      FlowProcessService processService = changeBranch(currentFlow,flowProcessVO);

      // 检查线路
      FlowPath flowPath = (FlowPath)baseDao.getObjectBySerializableKey(FlowPath.class,flowProcessVO.getInstanceId());

      // 创建线路信息并且标注为 MASTER
      if(flowPath == null){

        flowPath = new FlowPath();
        flowPath.setFlowInstanceId(flowProcessVO.getInstanceId());
        flowPath.setMasterFlowId(flowProcessVO.getFlowId());
        flowPath.setChangePathNodeId(null);
        flowPath.setStatus("MASTER");
        baseDao.saveOrUpdate(flowPath);
        baseDao.currentSessionflush();
      }



      // 获取当前节点ID
      String currentNodeId =
         getCurrentNodeId(currentFlow.getId(), flowPath.getFlowInstanceId());

      // 获取当前节点
      FlowNodeEntity currentNode =
        (FlowNodeEntity)baseDao.getObjectBySerializableKey(FlowNodeEntity.class,currentNodeId);

      // 获取当前用户
      UserEntity currentUser =
        (UserEntity)baseDao.getObjectBySerializableKey(UserEntity.class,AppTools.GetCurrentUserId());

      // 流程操作日志
      FlowProcessEntity flowProcessEntity =
        (FlowProcessEntity)flowProcessVO.bindEnity(FlowProcessEntity.class);

      flowProcessEntity.setCreateTime(new Date());
      flowProcessEntity.setCreateUserId(AppTools.GetCurrentUserId());
      flowProcessEntity.setCurrentNodeId(currentNodeId);
      flowProcessEntity.setCurrentNodeName(currentNode.getName());
      flowProcessEntity.setCurrentNodeType(currentNode.getType());
      flowProcessEntity.setCreateUserName(currentUser.getName());
      //审核信息增加企业信息
      SqlParams sqlParams = SqlParams.New();
      sqlParams.append("select a.epName from UserEntity a where a.id =:userId ");
      sqlParams.add("userId",AppTools.GetCurrentUserId());
      String eName = (String) baseDao.execSingleQuery(sqlParams.getSql(),sqlParams.getParams());
      flowProcessEntity.setEnterPriseName(eName);


      // 获取节点列表
      List<FlowNodeEntity> nodeList =
        getFlowInstanceNextNode(flowPath.getCurrentFlowId(), flowProcessEntity.getCurrentNodeId());

      String nextNodeId = nodeList != null && nodeList.size() > 0 ? nodeList.get(0).getId() : null;

      if(nextNodeId == null){

        nodeList = getFlowInstanceNextNode(flowPath.getCurrentFlowId(), flowPath.getChangePathNodeId());

        nextNodeId = nodeList != null && nodeList.size() > 0 ? nodeList.get(0).getId() : null;

        if(nextNodeId == null){
            nextNodeId = getFlowFirstNodeId(flowPath.getCurrentFlowId());
        }
      }

      flowProcessEntity.setNextNodeId(nextNodeId);
      // 打回
      if("BACK".equals(flowProcessVO.getType())){
        flowProcessEntity.setNextNodeId(getFlowFirstNodeId(flowPath.getMasterFlowId()));
        flowProcessEntity.setFlowId(flowPath.getMasterFlowId());
        flowPath.setStatus("MASTER");
        baseDao.saveOrUpdate(flowPath);
        baseDao.currentSessionflush();
      }

      // 获取下一节点信息
      FlowNodeEntity nextNode =
        (FlowNodeEntity)baseDao.getObjectBySerializableKey(FlowNodeEntity.class,flowProcessEntity.getNextNodeId());

      FlowNodeEntity curtNode =
              (FlowNodeEntity)baseDao.getObjectBySerializableKey(FlowNodeEntity.class,flowProcessEntity.getCurrentNodeId());

      flowProcessEntity.setNextNodeName(nextNode.getName());

      FlowInstanceEntity flowInstanceEntity = new FlowInstanceEntity();
      flowInstanceEntity.setId(flowProcessEntity.getInstanceId());
      flowInstanceEntity.setFlowId(flowPath.getCurrentFlowId());
      flowInstanceEntity.setCurrentNodeId(currentNodeId);
      flowInstanceEntity.setNextNodeNodeId(flowProcessEntity.getNextNodeId());

      String array[] = nextNode.getRoleCode().split(",");
      if(array.length  > 1){
        for (int i = 0; i < array.length; i++) {
          flowInstanceEntity.setRoleCode(array[i]);
          if(array[i].equals(AppTools.getCurrentRoleGroupId())){
            break;
          }
        }
      }else {
        flowInstanceEntity.setRoleCode(nextNode.getRoleCode());
      }
      flowInstanceEntity.setNodeName(nextNode.getName());
      flowInstanceEntity.setNodeType(nextNode.getType());

      // 设置流程实例状态 下一节点若为结束则结束流程 否则为操作动作描述
      if("END".equals(nextNode.getType())){
        flowInstanceEntity.setStatus("END");
      } else {
        flowInstanceEntity.setStatus(flowProcessVO.getType());
      }
      flowInstanceEntity.setCreateTime(new Date());
      flowInstanceEntity.setCreateUserId(AppTools.GetCurrentUserId());

      try {
        // 节点监控处理
        if(processService != null){

          FlowInstanceBean flowInstanceBean =
            (FlowInstanceBean)BeanTools.BindEnityUnValidate(flowInstanceEntity,FlowInstanceBean.class);

          FlowNodeVO cNode = (FlowNodeVO)BeanTools.BindEnityUnValidate(currentNode,FlowNodeVO.class);
          FlowNodeVO nNode = (FlowNodeVO)BeanTools.BindEnityUnValidate(nextNode,FlowNodeVO.class);

          if(!processService.filter(flowInstanceBean,cNode,nNode)){
            log.warn("警告: 流程监控拒绝流程操作 实例ID: [{}]",flowInstanceBean.getId());
            ErrorMsg.FLOW_FILTER.Ex();
          }
        }
      }catch (Exception e){
        e.printStackTrace();
        ErrorMsg.FLOW_FILTER.Ex();
      }
      baseDao.saveOrUpdate(flowInstanceEntity);
      baseDao.saveObject(flowProcessEntity);

      log.info("【flowProcessEntity】{}",flowProcessEntity);

    }catch (Exception e){
      e.printStackTrace();
      throw new AppException(e.getMessage());
    }
  }
}
